package eclMain;
import gui.ChatListener;

import java.util.ArrayList;

import client.ECMClient;

/**This Class is basically an api between the different programs. It will transmit ChatLines between the reader, the server, the local client window, and whatever else I come up with*/
public class Notifier {
	private static ECMClient ecmc = null;
	private static ArrayList<ChatListener> listenerList = new ArrayList<ChatListener>();
	
	/**This method accepts all chat lines and sends them to the server, and to the local client*/
	public static synchronized void notifyOfNewChat(ArrayList<ChatLine> lines) {
		for (ChatLine line : lines) {
			if (GlobalSettings.allowSendToLocalUI) {
//				System.out.println(line);
				for (ChatListener listener : listenerList) {
					listener.notifyOfChat(line);
				}
			}
			if (GlobalSettings.allowSendToServer) {
				if (ecmc == null) ecmc = new ECMClient(); //start the client connection if it doesn't exist yet
				ecmc.send(line);
			}
			System.out.println("Notifier: " + line); //XXX: debug to console
		}
	}
	
	public static synchronized void addUIListener(ChatListener listener) {
		listenerList.add(listener);
	}
	
	public static synchronized void removeUIListener(ChatListener listener) {
		listenerList.remove(listener);
	}
	
	//TODO: print these messages to log files
	/**Send an error message to the console*/
	public static synchronized void errorNotify(String message) {
		System.err.println("err:" + message);
	}
	
	/**Print a standard message to the console*/
	public static synchronized void standardNotify(String message) {
		System.out.println("std:" + message);
	}
}
